import { _decorator, Component, Node, Vec3, Label } from "cc";
import { getGlobal, MyGlobal } from "../Global";
const { ccclass, property } = _decorator;
import { Utils } from "../utils/utils";
@ccclass("Alert")
export class Alert extends Component {
  @property(Node)
  public alert: Node = null;
  @property(Node)
  btnOK: Node;
  @property(Node)
  btnCancel: Node;
  @property(Label)
  title: Label;
  @property(Label)
  content: Label;
  _onok: any;
  onLoad() {
    // this._alert = cc.find("Canvas/alert");
    // this._title = cc.find("Canvas/alert/title").getComponent(cc.Label);
    // this._content = cc.find("Canvas/alert/content").getComponent(cc.Label);

    // this._btnOK = cc.find("Canvas/alert/btn_ok");
    // this._btnCancel = cc.find("Canvas/alert/btn_cancel");

    Utils.addClickEvent(this.btnOK, this.node, "Alert", "onBtnClicked");
    Utils.addClickEvent(this.btnCancel, this.node, "Alert", "onBtnClicked");
    let g = getGlobal();

    this.alert.active = false;
    // cc.vv.alert = this;
    g.alert = this;
  }
  start() {}

  update(deltaTime: number) {}

  onBtnClicked(event) {
    if (event.target.name == "btn_ok") {
      if (this._onok) {
        this._onok();
      }
    }
    this.alert.active = false;
    this._onok = null;
  }

  show(title, content, onok = null, needcancel = false) {
    this.alert.active = true;
    this._onok = onok;
    this.title.string = title;
    this.content.string = content;
    console.log(arguments, "?show alert");
    if (needcancel) {
      this.btnCancel.active = true;
      // this.btnOK.x = -150;
      this.btnOK.setPosition(new Vec3(-150));
      // this.btnCancel.x = 150;
      this.btnCancel.setPosition(new Vec3(-150));
    } else {
      this.btnCancel.active = false;
      this.btnOK.setPosition(new Vec3(0));
      // this.btnOK.x = 0;
    }
  }

  onDestory() {
    let g = getGlobal();
    g.alert = null;
  }
}
